<template>
  <div>
    <basic-layout title="预约记录"
                  :showFilterBtn="true"
                  :showAddBtn="true"
                  @handleAdd="handleAdd"
                  @handleFilter="$refs.queryPopupRef.show = true">
      <div class="body_box">
        <van-row class="listCon">
          <van-pull-refresh
            class="vanScroller"
            v-model="refreshing"
            @refresh="onRefresh">
            <van-list
              class="vanList"
              v-model="loading"
              :finished="finished"
              :finished-text="listData.length ? '没有更多了' : ''"
              @load="onLoad"
            >
              <div v-for="(item , index) in listData" :style="{ padding: calcP(index), }">
                <div class="main">
                  <div class="main1" @click="detail(item)">
                    <div>
                      {{ item.createTime }}
                    </div>
                    <div v-if="item.orderStatus == '4' && item.isPropertyReturn == '1'"
                         style="color: #d7a653">
                      已取消(由服务点退回)
                    </div>
                    <div v-else style="color: #d7a653">
                      {{ item.orderStatusLabel }}
                    </div>
                  </div>
                  <div class="main2" @click="detail(item)">
                    <div class="main2_1">
                      <div class="picStyle" v-if="item.srvitemType == '1'"/>
                      <div class="picStyle_liliao" v-if="item.srvitemType == '2'"/>
                      <div class="picStyle_water" v-if="item.srvitemType == '3'"/>
                      <div class="picStyle_clean" v-if="item.srvitemType == '4'"/>
                      <div class="picStyle_laundry" v-if="item.srvitemType == '5'"/>
                      <div class="picStyle_xiche" v-if="item.srvitemType == '6'"/>
                      <div class="picStyle_banyun" v-if="item.srvitemType == '7'"/>
                      <div class="picStyle_lvhua" v-if="item.srvitemType == '8'"/>
                      <div class="picStyle_main2">
                        <div class="picStyle_main2_1">
                          {{ item.srvitemTypeLabel }}
                        </div>
                        <div class="picStyle_main2_2">
                          预约时间:<span>{{ calcServiceDate(item) }}</span>
                        </div>
                        <div class="picStyle_main2_3" v-if="item.propertySrvAppRule && item.propertySrvAppRule.isCancelApp == '1'">
                          请您按时接受服务，行程有变请提前<span>{{ item.propertySrvAppRule.cancelAdvanceTimeLimit }}</span>分钟取消
                        </div>
                        <div class="picStyle_main2_3" v-else>
                          如需取消，请联系服务点 {{ item.saProperty.phone }}
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="main3" v-if="item.orderStatus == '1' && (item.propertySrvAppRule && item.propertySrvAppRule.isCancelApp == '1')">
                    <div class="main3_1">
                      <button @click="cancelApp(item)" class="main3_1_button">
                        取消预约
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            </van-list>
            <van-empty
              v-if="!loading && !listData.length"
              class="custom-image"
              :image="require('../../../../assets/images/empty.png')"
              description="暂无数据"
            />
          </van-pull-refresh>
        </van-row>

      </div>
    </basic-layout>


    <!-- 筛选条件弹出框 -->
    <queryPopup ref="queryPopupRef" @handleSearch="onRefresh" @handleReset="handleReset">
      <van-form class="filterBox">
        <label>服务点</label>
        <van-field>
          <template #input>
            <div class="textCon" @click="showProperty = true">
              <span v-if="!propertyNameText" style="color: #aaa">请选择服务点</span>
              <span v-else>{{ propertyNameText }}</span>
            </div>
          </template>
        </van-field>
        <label>订单状态</label>
        <van-field>
          <template #input>
            <div class="textCon" @click="showOrderStatus = true">
              <span v-if="!orderStatusText" style="color: #aaa">请选择订单状态</span>
              <span v-else>{{ orderStatusText }}</span>
            </div>
          </template>
        </van-field>
        <label>订单号</label>
        <van-field v-model="queryParam.orderNo"/>
        <label>预约日期</label>
        <van-field>
          <template #input>
            <div class="textCon" @click="showServiceDate = true">
              <span v-if="!queryParam.serviceBeginDate" style="color: #aaa">请选择预约日期</span>
              <span v-else>{{ queryParam.serviceBeginDate + '~' + queryParam.serviceEndDate }}</span>
            </div>
          </template>
        </van-field>
        <label>订单时间</label>
        <van-field>
          <template #input>
            <div class="textCon" @click="showCreateTime = true">
              <span v-if="!queryParam.createTimeBegin" style="color: #aaa">请选择订单时间</span>
              <span v-else>{{ queryParam.createTimeBegin + '~' + queryParam.createTimeEnd }}</span>
            </div>
          </template>
        </van-field>
      </van-form>
    </queryPopup>
    <!-- 服务点选择 -->
    <van-action-sheet v-model="showProperty" cancel-text="取消" :actions="propertyList" @select="onConfirmProperty"/>
    <van-action-sheet v-model="showOrderStatus" cancel-text="取消" :actions="orderStatusList"
                      @select="onConfirmOrderStatus"/>
    <van-calendar
      :min-date="minDate"
      :max-date="maxDate"
      :allow-same-day="true"
      :show-confirm="false"
      v-model="showServiceDate"
      type="range"
      @confirm="onConfirmServiceDate"/>
    <van-calendar
      :min-date="minDate"
      :max-date="maxDate"
      :allow-same-day="true"
      :show-confirm="false"
      v-model="showCreateTime"
      type="range"
      @confirm="onConfirmCreateTime"/>
  </div>
</template>
<script>
import {getPropertyOrderListForApp} from "@api/srvAssurance/emplcare/apply/order";
import {getAppUserProperty, getPropertyConfig} from "@api/srvAssurance/emplcare/apply/index";
import {queryDic} from "@api/common";
import {formateBeginAndEndDate, calcServiceDate} from "@/plugins/utils/formatTime";

export default {
  name: 'appList',
  props: {},
  components: {},
  data() {
    return {
      srvitemType: '',
      refreshing: false,
      loading: false,
      finished: false,
      listData: [],

      minDate: new Date(new Date().getFullYear() - 1, 0, 1),
      maxDate: new Date(new Date().getFullYear(), 11, 31),

      propertyList: [],
      srvitemTypeList: [],
      orderStatusList: [],
      showProperty: false,
      propertyNameText: '',
      showSrvitemType: false,
      srvitemTypeText: '',
      showOrderStatus: false,
      orderStatusText: '',
      showServiceDate: false,
      serviceDateRange: {
        begin: '',
        end: '',
      },
      showCreateTime: false,
      createTimeRange: {
        begin: '',
        end: '',
      },

      queryParam: {
        propertyId: '',
        srvitemType: '',
        orderStatus: '',
        orderNo: '',
        serviceDateTemp: '',
        serviceBeginDate: '',
        serviceEndDate: '',
        createTimeTemp: '',
        createTimeBegin: '',
        createTimeEnd: '',
        corpId: '',
        deptId: '',
        pageNum: 1,
        pageSize: 10,
        totalNum: 0,
      },
    }
  },
  created() {
    this.queryDicData();
  },
  computed: {},
  beforeMount() {
    this.queryParam.srvitemType = this.$route.query.srvitemType;
    this.getList();
  },
  methods: {
    onConfirmProperty(item) {
      this.queryParam.propertyId = item.propertyId;
      this.showProperty = false;
      this.propertyNameText = item.propertyName;
      this.onRefresh();
    },

    openSrvitemType() {
      if (!this.propertyNameText) {
        this.$Notify({type: 'success', message: '请选选择服务点！'});
        return;
      }
      this.showSrvitemType = true;
    },

    onConfirmSrvitemType(item) {
      this.queryParam.srvitemType = item.value;
      this.showSrvitemType = false;
      this.srvitemTypeText = item.valueAlias;
      this.onRefresh();
    },

    onConfirmOrderStatus(item) {
      this.queryParam.orderStatus = item.value;
      this.showOrderStatus = false;
      this.orderStatusText = item.valueAlias;
      this.onRefresh();
    },

    onConfirmServiceDate(item) {
      let formateDate = formateBeginAndEndDate(item, "1");
      this.queryParam.serviceBeginDate = formateDate[0];
      this.queryParam.serviceEndDate = formateDate[1];
      this.showServiceDate = false;
    },

    onConfirmCreateTime(item) {
      let formateDate = formateBeginAndEndDate(item, "1");
      this.queryParam.createTimeBegin = formateDate[0];
      this.queryParam.createTimeEnd = formateDate[1];
      this.showCreateTime = false;
    },

    calcServiceDate(item) {
      return calcServiceDate(item.serviceBeginDate, item.serviceEndDate, ".");
    },

    calcP(index) {
      if (index == 0) {
        return '10px 0px';
      } else {
        return '0px 0px 10px 0px';
      }
    },

    detail(item) {
      let paramData = {
        srvOrderId: item.srvOrderId,
      }

      if (sessionStorage.getItem("emp_care_apply_config")) {
        this.$router.push({
          path: "/emplcare/appDetailExtend",
          query: {param: paramData}
        })
      } else {
        this.$router.push({
          path: "/emplcare/appDetail",
          query: {param: paramData}
        })
      }
      // this.$router.push({
      //   path: "/emplcare/appDetail",
      //   query: {param: paramData}
      // })
    },

    // 重置
    handleReset() {
      this.queryParam = {
        propertyId: '',
        srvitemType: this.queryParam.srvitemType,
        orderStatus: '',
        orderNo: '',
        serviceDateTemp: '',
        serviceBeginDate: '',
        serviceEndDate: '',
        createTimeTemp: '',
        createTimeBegin: '',
        createTimeEnd: '',
        corpId: '',
        deptId: '',
        pageNum: 1,
        pageSize: 10,
        totalNum: 0,
      };
      this.showProperty = false;
      this.propertyNameText = '';
      this.showSrvitemType = false;
      this.srvitemTypeText = '';
      this.showOrderStatus = false;
      this.orderStatusText = '';
      this.showServiceDate = false;
      this.serviceDateRange = {
        begin: '',
        end: '',
      };
      this.showCreateTime = false;
      this.createTimeRange = {
        begin: '',
        end: '',
      };
      this.onRefresh();
    },

    onLoad() {
      this.queryParam.pageNum++;
      this.getList();
    },

    onRefresh() {
      this.refreshing = false;
      this.finished = false;
      this.listData = [];
      this.queryParam.pageNum = 1;
      this.getList();
    },

    handleAdd() {
      let i = this.srvitemTypeList.filter(i => i.value == this.queryParam.srvitemType)[0];
      let paramData = {
        srvitemType: this.queryParam.srvitemType,
        srvitemTypeLabel: i.valueAlias,
      }
      this.$router.push({
        path: "/emplcare/applyManage",
        query: {param: paramData}
      })
    },

    async getList() {
      this.loading = true;
      await getPropertyOrderListForApp(this.queryParam).then((res) => {
        this.loading = false;
        if (res.code === 1000 && res.data) {
          this.listData = [...this.listData, ...res.data.records];
          if (this.listData.length >= res.data.total) {
            this.finished = true;
          }
        } else {
          this.finished = true;
        }
      }).catch(() => {
        this.loading = false;
        this.finished = true;
      });
    },

    cancelApp(item) {
      let paramData = {
        srvOrderId: item.srvOrderId
      }
      this.$router.push({
        path: "/emplcare/confirmCancelApp",
        query: {param: paramData}
      })
    },

    // 下拉框数据
    queryDicData() {
      getAppUserProperty().then(res => {
        if (res.code === 1000 && res.data) {
          res.data.forEach(item => {
            item.name = item.propertyName;
          })
          this.propertyList = res.data;
        }
      });
      queryDic('SA_SERVICE_ITEM_TYPE_CODE').then(res => {
        if (res.code === 1000 && res.data) {
          this.srvitemTypeList = res.data;
        }
      });
      queryDic('SA_SERVICE_ORDER_STATUS_CODE').then(res => {
        if (res.code === 1000 && res.data) {
          this.orderStatusList = res.data;
        }
      });

      sessionStorage.setItem("emp_care_apply_config", false)
      getPropertyConfig().then(res => {
        if (res.code === 1000 && res.data) {
          sessionStorage.setItem("emp_care_apply_config", res.data)
        }
      });
    }

  },
}
</script>
<style lang="less" scoped>

.body_box {
  background-color: #f9f9f9;
  height: 100%;

  .listCon {
    height: 100%;

    .vanScroller {
      height: 100%;
      overflow-y: auto;

      &::-webkit-scrollbar {
        width: 0px !important;
        height: 0px !important;
      }

      .vanList {

        .main {
          display: flex;
          align-items: center;
          flex-direction: column;
          justify-content: space-between;
          background: rgb(255, 255, 255);
          color: rgb(51, 51, 51);
          border: 1px solid rgb(220, 223, 230);
          border-radius: 6px;
          height: 185px;
          padding: 10px;


          .main1 {
            height: 30px;
            line-height: 30px;
            display: flex;
            font-size: 15px;
            font-weight: 600;
            justify-content: space-between;
            width: 100%;
          }

          .main2 {
            height: 120px;
            width: 100%;
            padding: 10px 0px;

            .main2_1 {
              display: flex;
              justify-content: space-between;
              align-items: center;
              height: 100px;


              .picStyle {
                background-image: url('../../../../assets/images/care/hair_cut.png');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }

              .picStyle_liliao {
                background-image: url('../../../../assets/images/care/li_liao.jpg');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }

              .picStyle_water {
                background-image: url('../../../../assets/images/care/water.jpg');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }


              .picStyle_clean {
                background-image: url('../../../../assets/images/care/cleaning.jpg');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }


              .picStyle_laundry {
                background-image: url('../../../../assets/images/care/xiyi.png');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }


              .picStyle_xiche {
                background-image: url('../../../../assets/images/care/xiche.jpg');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }


              .picStyle_banyun {
                background-image: url('../../../../assets/images/care/banyun.jpg');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }

              .picStyle_lvhua {
                background-image: url('../../../../assets/images/care/lvhua.jpg');
                background-repeat: no-repeat;
                background-size: 100% 100%;
                height: 100px;
                width: 100px;
                background-color: #f9f9f9;
                color: rgb(51, 51, 51);
                border: 1px solid rgb(220, 223, 230);
                border-radius: 6px;
              }


              .picStyle_main2 {
                display: flex;
                align-items: flex-start;
                height: 100%;
                width: calc(100% - 100px);
                margin-left: 10px;
                flex-direction: column;
                justify-content: space-between;

                .picStyle_main2_1 {
                  font-size: 16px;
                  font-weight: 600;
                }

                .picStyle_main2_2{
                  color: rgb(152, 153, 153);
                }

                .picStyle_main2_3{
                  color: rgb(152, 153, 153);
                }
              }

            }
          }

          .main3 {
            width: 100%;
            height: 40px;

            .main3_1 {
              width: 100%;
              display: flex;
              flex-direction: row-reverse;

              .main3_1_button {
                color: rgb(217, 165, 83);
                border: 0.5px solid rgb(217, 165, 83);
                border-radius: 40px;
                height: 40px;
                line-height: 40px;
                width: 85px;
                font-size: 13px;
                background-color: rgb(255, 255, 255);
              }
            }
          }
        }

      }
    }

  }
}


</style>
